<?php
	class Users extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->model(array('user', 'article', 'photo', 'video', 'komentar'));
			$this->load->helper(array('dharma_date_helper', 'dharma_video_helper'));
			$this->userdata = $this->session->userdata('userdata');
			if($this->userdata){
				$recent_articles = $this->article->get_article(NULL, NULL, 5);
				$recent_photos = $this->photo->get_foto(NULL, NULL, 6);
			}else{
				$recent_articles = $this->article->get_article(NULL, NULL, 5, 0, 'public');
				$recent_photos = $this->photo->get_foto(NULL, NULL, 6, 0, 'public');
			}
			$this->right_sidebar = array('categories' => unserialize(ARTICLE_CATEGORY),
										 'recent_articles' => $recent_articles,
										 'recent_photos' => $recent_photos);
		}
		
		public function index($id){
			if(count($this->article->get_article(NULL, $id)) > 4){
				$status_a = 'yes';
			}else{
				$status_a = NULL;
			}
			if(count($this->photo->get_foto(NULL, $id)) > 4){
				$status_p = 'yes';
			}else{
				$status_p = NULL;
			}
			if(count($this->video->get_video(NULL, $id)) > 4){
				$status_v = 'yes';
			}else{
				$status_v = NULL;
			}
			$data = array('user'				=> $this->user->get_alumni($id),
						  'session' 			=> $this->userdata,
						  'articles' 			=> $this->article->get_article(NULL, $id, 4),
						  'photos'				=> $this->photo->get_foto(NULL, $id, 4),
						  'videos'				=> $this->video->get_video(NULL, $id),
						  'more_article_status' => $status_a,
						  'more_photo_status' 	=> $status_p,
						  'more_video_status' 	=> $status_v,
						  'footer'				=> $this->right_sidebar);
			$this->load->view('user/index', $data);
		}
		
		public function check_username_exist(){
			$usr = $this->input->post('name');
			$id = $this->input->post('id');		
			$result = $this->user->check_user_exist($usr, $id);
			if($result){
				echo json_encode("false");
			}else{
				echo json_encode("true");
			}
		}
		
		public function more_article($id){
			$data['articles'] = $this->article->get_article(NULL, $id);
			foreach($data['articles'] as $article){
				$com[] = $this->komentar->get_current_komentar('artikel', $article['id_artikel'], 'LIST_KOMENTAR');
			}
			$data['total_comments'] = $com;
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/index', $data);
		}
		
		public function more_photo($id){
			$data['photos'] = $this->photo->get_foto(NULL, $id);
			$data['footer'] = $this->right_sidebar;
			$this->load->view('photo/index', $data);
		}
		
		public function edit($id){
			$profile = $this->user->get_alumni($this->userdata['id_user']);
			if(isset($_POST['profile_submit'])){
				//edit user avatar
				$random           = rand(000000,999999);
				$image_upload 	  = $_FILES['user_avatar']['name'];
				if ($image_upload != null) {
					$url = './images/user_avatars/'.$profile[0]['foto'];
					$config['upload_path'] = './images/user_avatars/';
					$config['allowed_types'] = 'jpg|png|gif|jpeg';
					if (isset($url)) {
						if($remove = unlink($url)){
							$config['file_name'] = $random."_".$image_upload;
							$this->load->library('upload', $config);
							if (!$this->upload->do_upload('user_avatar', TRUE)) { // upload image to server
								$error = $this->upload->display_errors();
								echo $error;
							} 
						}else{
							echo "not removed"; die;
						}
					}
					$alumni_post_data = array(
									'nama' => $this->input->post('nama'),
									'jk' => $this->input->post('jk'),
									'pekerjaan' => $this->input->post('pekerjaan'),
									'alamat' => $this->input->post('alamat'),
									'telepon' => $this->input->post('telepon'),
									'email' => $this->input->post('email'),
									'hp' => $this->input->post('hp'),
									'status' => $this->input->post('status'),
									'foto' => $random."_".strtolower($image_upload)
									);
				}else{
					$alumni_post_data = array(
									'nama' => $this->input->post('nama'),
									'jk' => $this->input->post('jk'),
									'pekerjaan' => $this->input->post('pekerjaan'),
									'alamat' => $this->input->post('alamat'),
									'telepon' => $this->input->post('telepon'),
									'email' => $this->input->post('email'),
									'hp' => $this->input->post('hp'),
									'status' => $this->input->post('status')
									);
				}
				$pass = $this->input->post('password');
				if($pass != NULL){
					$user_post_data = array('username' => $this->input->post('username'),'password' => md5($pass));				
				}else{
					$user_post_data = array('username' => $this->input->post('username'));		
				}
				$this->user->edit($id, $user_post_data);
				$this->user->edit_alumni($id, $alumni_post_data);
				redirect(site_url('/users/index/'.$id));
			}
			$data['footer'] = $this->right_sidebar;
			$data['user'] = $profile;
			$this->load->view('user/form', $data);
		}
	}
?>